package com.base.common.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.common.dao.MessageDAO;
import com.base.model.MessageInfo;

public class MessageServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = (String)req.getParameter("method");
		if(method != null){
			if("load".equals(method)){
				this.load(req, resp);
			}else if("list".equals(method)){
				this.list(req, resp);
			}else if("insert".equals(method)){
				this.insert(req, resp);
			}else if("update".equals(method)){
				this.update(req, resp);
			}else if("delete".equals(method)){
				this.delete(req, resp);
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	protected void load(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String messageId = (String)req.getParameter("messageId");
		MessageDAO dao = new MessageDAO();
		MessageInfo entity = null;
		entity = dao.load(messageId);
		entity.setMessageViewFlag("1");
		dao.update(entity);
		if(entity != null){
			req.setAttribute("entity", entity);
			RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/message/messageInfo.jsp");
			rd.forward(req, resp);
		} else{
			resp.sendRedirect("pages/admin/admin.jsp");
			return;
		}
	}
	
	protected void list(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		MessageDAO dao = new MessageDAO();
		List<MessageInfo> results = (List<MessageInfo>)dao.list();
		if(results != null){
			req.setAttribute("list", results);
		}else{
			req.setAttribute("list", null);
		}
		RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/message/messageManage.jsp");
		rd.forward(req, resp);
	}
	
	protected void insert(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String messageSubject = (String)req.getParameter("messageSubject");
		String messageContent = (String)req.getParameter("messageContent");
		String contactName = (String)req.getParameter("contactName");
		String contactEmail = (String)req.getParameter("contactEmail");
		String contactTel = (String)req.getParameter("contactTel");
		String contactSex = (String)req.getParameter("contactSex");
//		String messageViewFlag = (String)req.getParameter("messageViewFlag");
//		String createUserId = (String)req.getParameter("createUserId");
//		String updateUserId = (String)req.getParameter("updateUserId");
		
		MessageDAO dao = new MessageDAO();
		
		MessageInfo entity = new  MessageInfo();
		entity.setMessageId(entity.getUUID());
		entity.setMessageSubject(messageSubject);
		entity.setMessageContent(messageContent);
		entity.setContactEmail(contactEmail);
		entity.setContactName(contactName);
		entity.setContactSex(contactSex);
		entity.setContactTel(contactTel);
		entity.setMessageViewFlag("0");
//		entity.setCreateUserId(createUserId);
//		entity.setUpdateUserId(updateUserId);
		
		boolean success = dao.insert(entity);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/pages/guest/messageInfo.jsp");
		rd.forward(req, resp);
	}
	
	protected void update(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String messageId = (String)req.getParameter("messageId");
	}
	
	protected void delete(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String messageId = (String)req.getParameter("messageId");
		MessageDAO dao = new MessageDAO();
		
		boolean success = dao.delete(messageId);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/MessageServlet.do?method=list");
		rd.forward(req, resp);
	}
}
